# -*- coding: utf-8 -*-
"""
mathmodeling_SAK.programming.misc
=================================

Pyomo 小助手工具杂项扩展包
"""

import tkinter as tk
import threading

class ModelRunnerNotifier:
    """
    用于求解模型时的警示标语弹出，警告电脑屏幕周围的人该电脑正在运行模
    型并且不要乱动这台电脑。

    输入参数
    ----------
    large_text : str, optional
        窗口中显示的大标签的字样，default by "在跑模型"
    small_text : str, optional
        窗口显示的小标签的字样，default by "别碰我的电脑"
    window_geometry : str, optional
        调整窗口的大小。default by "900x600".
    large_text_size : int, optional
        窗口中显示的大标签的字体大小，default by 150.
    small_text_size : int, optional
        窗口显示的小标签的字体大小，default by 75.
        
    用法示例
    ----------
    
    首先需要创建通知器实例：
    
    >>> notifier = ModelRunnerNotifier()

    在调用求解器之前弹出消息窗口：
    
    >>> notifier.show_window()

    创建 Pyomo 模型：
    
    >>> model = ConcreteModel()
    >>> model.x = Var(within=NonNegativeReals)
    >>> model.y = Var(within=NonNegativeReals)
    >>> model.obj = Objective(
            expr=model.x + model.y, sense=minimize)
    >>> model.con = Constraint(
            expr=model.x + 2 * model.y >= 1)
    
    使用 glpk 求解器求解模型：

    >>> solver = SolverFactory('glpk')
    >>> solver.solve(model)

    模型运行完毕后，关闭窗口：
    
    >>> notifier.close_window()
    """

    def __init__(
            self, 
            large_text="在跑模型", 
            small_text="别碰我的电脑",
            window_geometry="900x600",
            large_text_size=150,
            small_text_size=75):
        self.large_text = large_text
        self.small_text = small_text
        self.large_text_size = large_text_size
        self.small_text_size = small_text_size
        self.root = None
        self.window_thread = None

    def show_window(self):
        def _show_window():
            self.root = tk.Tk()
            self.root.title("提示")
            self.root.geometry("900x600")

            label_large = tk.Label(
                self.root, 
                text=self.large_text, 
                font=("Arial", self.large_text_size)
                )
            label_large.pack(pady=20)

            label_small = tk.Label(
                self.root, 
                text=self.small_text, 
                font=("Arial", self.small_text_size)
                )
            label_small.pack(pady=10)

            self.root.mainloop()

        self.window_thread = threading.Thread(
            target=_show_window)
        self.window_thread.start()

    def close_window(self):
        if self.root is not None:
            self.root.quit()
            self.window_thread.join()
